// Copyright 2022 jeff.li. and/or its affiliates.

#include <tbir/runtime/function_name_rules.h>

#include <string>
#include <vector>

#include <tbir/runtime/logging.h>

namespace tbir::runtime::FunctionNameRules {

    static string_view class_method_sep("__F_");

    String get_class_view_name(string_view class_name) {
        return String(class_name) + String("_SharedView");
    }

    String add_wrapper_suffix(string_view function_name) {
        return String::Concat(function_name, "_wrapper");
    }

    String add_packed_suffix(string_view function_name) {
        return String::Concat(function_name, "__c_api");
    }

    String add_class_prefix(string_view class_name, string_view method_name) {
        return String(class_name) + String("__F_") + String(method_name);
    }

    string_view remove_class_prefix(string_view class_name, string_view method_name) {
        MXCHECK(is_class_method(class_name, method_name));
        return method_name.substr(class_name.size() + class_method_sep.size());
    }

    bool is_class_method(string_view class_name, string_view method_name) {
        return method_name.size() > (class_name.size() + class_method_sep.size()) &&
               method_name.substr(0, class_name.size()) == class_name &&
               method_name.substr(class_name.size(), class_method_sep.size()) == class_method_sep;
    }

}  // namespace tbir::runtime::FunctionNameRules
